package com.foreveross.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.service.LogService;
import com.foreveross.service.ProductTypeService;
import com.foreveross.util.AuthUtil;
import com.foreveross.vo.HouseUser;
import com.foreveross.vo.Log;
import com.foreveross.vo.ProductType;
import com.foreveross.vo.common.PageVo;
import com.foreveross.vo.common.ResultVo;

@Controller
@RequestMapping(value = "/sysProductTypeController")
public class SysProductTypeController  extends BaseCostroller{

	private Logger log = Logger.getLogger(SysProductTypeController.class);

	@Resource
	private ProductTypeService productTypeService;

	@Resource
	private LogService logService;

	/**
	 * 查看产品类型
	 * 
	 * @param request
	 * @param response
	 * @param OnePageCount
	 * @param currentPage
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/findProductTypeList")
	public void findProductTypeList(HttpServletRequest request,
			HttpServletResponse response, String rows, String page,
			String proName, String proLineName, String proCode) {
		ResultVo resultVo = new ResultVo();
		try {
			int countRecord = (int) productTypeService.getProductTypeListCount(
					proCode, proName, proLineName);
			PageVo<ProductType> pageObj = new PageVo<ProductType>(page,
					countRecord, rows);
			pageObj.setRows(productTypeService.findProductTypeList(proCode, proName,
					proLineName, null, null));
			resultVo.setData(pageObj);
			resultVo.setSuccess(true);
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("产品类型 查看失败");
			log.error("产品类型 查看失败!", e);
		}
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	/**
	 * 查询产品类型
	 * @param request
	 * @param response
	 * @param proCode
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/findProductTypeSelect")
	public void findProductTypeSelect(HttpServletRequest request,
			HttpServletResponse response, String proCode) {
		List<ProductType> list = new ArrayList<ProductType>();
		try {
			list = productTypeService.findProductTypeSelect(proCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		CommonUtil.PrintJSONSTR(response, JSONArray.fromObject(list).toString());
	}
	
	/**
	 * 查询产品系列
	 * @param request
	 * @param response
	 * @param proCode
	 * @param proLineCode
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/findProductTypeLineSelect")
	public void findProductTypeLineSelect(HttpServletRequest request,
			HttpServletResponse response, String proCode,String proLineCode) {
		List<ProductType> list = new ArrayList<ProductType>();
		try {
			list = productTypeService.findProductTypeLineSelect(proCode,proLineCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
		CommonUtil.PrintJSONSTR(response, JSONArray.fromObject(list).toString());
	}

	/**
	 * 产品类型添加修改
	 * 
	 * @param request
	 * @param response
	 * @param express
	 * @param updateType
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/addOrUpdateProductType")
	public void addOrUpdateProductType(HttpServletRequest request,
			HttpServletResponse response, ProductType productType,
			String updateType) {
		ResultVo resultVo = new ResultVo();
		try {
			productTypeService.saveOrUpdate(productType, updateType);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,
					updateType.equals(Constants.UPDATE_ADD) ? Constants.LOG_ADD
							: Constants.LOG_UPDATE,
					"产品类型" + Constants.getUpdateType(updateType),
					AuthUtil.getUserid(request),
					Constants.getUpdateType(updateType) + "产品类型["
							+ productType.toString() + "]");
			log.info("产品类型 " + updateType + "成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("产品类型 " + updateType + "失败");
			log.error("产品类型" + updateType + "失败!", e);
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	/**
	 * 产品类型删除
	 * 
	 * @param request
	 * @param response
	 * @param ids
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/deleteProductTypeList")
	public void deleteProductTypeList(HttpServletRequest request,
			HttpServletResponse response, String ids) {
		ResultVo resultVo = new ResultVo();
		try {
			productTypeService.deleteProductTypeList(ids);
			resultVo.setSuccess(true);
			logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_DEL, "产品类型删除",
					AuthUtil.getUserid(request), "产品类型[" + ids + "]");
			log.info("产品类型 删除成功!");
		} catch (Exception e) {
			resultVo.setSuccess(false);
			resultVo.setMessage("产品类型 删除失败");
			log.info("产品类型 删除失败!");
			e.printStackTrace();
		}

		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

}
